<template>
    <div class="question">
        <img class="res-title"
             v-show="lock"
             :src="result ? 'common/img/right-title.png' : 'common/img/wrong-title.png'">
        <div class="title"
             :style="question.question.length < 22? 'text-align: center' : ''">{{question.question}}</div>
        <div class="answer-wrapper">
            <img class="q-img"
                 :src="question.image"
                 v-show="question.type === 'image'">
            <ul class="answer">
                <li v-for="(answer, index) in question.options"
                    :key="index"
                    :class="classTag + ' ' + (answer.select ? 'selected' : '')"
                    @click="toggleClick(index)"
                    class="option"><img class="key"
                         :src="`common/img/${answer.key.toUpperCase()}.png`">{{answer.value}}
                    <img class='result'
                         v-show="lock && (answer.select || question.answer.toUpperCase().indexOf(answer.key.toUpperCase()) >= 0)"
                         :src="question.answer.toUpperCase().indexOf(answer.key.toUpperCase()) < 0 ? 'common/img/wrong.png' : 'common/img/right.png'">
                </li>
            </ul>
        </div>
        <p v-show="complainShow"
           class="explain"><span style="font-weight: 900">解析：</span>{{question.complain}}</p>
        <img class="confirm"
             @click="confirm"
             src="./confirm.png">
    </div>
</template>

<script>
export default {
    name: "question",
    props: { question: Object },
    data() {
        return {
            maxLen: 0,
            lock: false,
            result: false,
            complainShow: false,
            selectedIndex: 0
        };
    },
    computed: {
        classTag() {
            if (this.maxLen > 20) {
                return "thick";
            } else if (this.maxLen > 7) {
                return "long";
            } else {
                return "";
            }
        }
    },
    methods: {
        toggleClick(index) {
            if (this.lock) return;
            if (this.question.type === "multi") {
                this.question.options[index].select = !this.question.options[
                    index
                ].select;
            } else {
                this.selectedIndex = index;
                this.question.options.map((item, item_index) => {
                    if (item_index == index) {
                        item.select = true;
                    } else {
                        item.select = false;
                    }
                });
            }
        },
        // 多选核查
        checkAnswerMulti() {
            // 组织选中数组 和 答案
            let answerList = this.question.options.filter(item => {
                return item.select;
            });
            let trueAnswer = this.question.answer.toUpperCase();

            // 长度不一致直接报错
            if (this.question.answer.length !== answerList.length) {
                return false;
            } else {
                for (var i = 0; i < answerList.length; i++) {
                    if (
                        trueAnswer.indexOf(answerList[i].key.toUpperCase()) < 0
                    ) {
                        return false;
                    }
                }
                return true;
            }
        },
        // 单选核查
        checkAnswerSingle() {
            let select = this.question.options[
                this.selectedIndex
            ].key.toUpperCase();
            return this.question.answer.toUpperCase().indexOf(select) >= 0;
        },
        // 核查
        confirm() {
            this.lock = true;
            // 多选则遍历判断
            this.result =
                this.question.type === "multi"
                    ? this.checkAnswerMulti()
                    : this.checkAnswerSingle();
            if (this.question.complain) {
                this.complainShow = true;
                setTimeout(() => {
                    this.$emit("answer-finish", this.result);
                }, 2000);
            } else {
                this.$emit("answer-finish", this.result);
            }
        }
    },
    created() {
        // console.log(this.question);
        this.question.options.map(item => {
            if (item.value.length > this.maxLen) {
                this.maxLen = item.value.length;
            }
        });
    }
};
</script>
<style lang="scss" scoped>
.question {
    height: 5rem;
    display: flex;
    flex-direction: column;
    .title {
        color: #21288a;
        font-size: 0.22rem;
        font-weight: bolder;
        line-height: 1.1;
    }
    .res-title {
        position: absolute;
        top: 2.5rem;
        left: 0;
        right: 0;
        margin: auto;
        height: 0.6rem;
    }
    .answer-wrapper {
        flex: 1;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        .q-img {
            width: 2.3rem;
        }
        .answer {
            .option {
                position: relative;
                margin: 0 auto 0.16rem;
                width: 2.07rem;
                height: 0.48rem;
                box-sizing: border-box;
                border-radius: 0.24rem;
                padding-left: 0.55rem;
                padding-right: 0.2rem;
                border: 0.02rem solid #db0025;
                display: flex;
                align-items: center;
                color: #3e3a39;
                font-size: 0.2rem;

                &:last-child {
                    margin-bottom: 0;
                }

                .key {
                    position: absolute;
                    left: 0.04rem;
                    top: 0;
                    bottom: 0;
                    margin: auto;
                    width: 0.38rem;
                }
                .result {
                    position: absolute;
                    right: -0.5rem;
                    top: 0;
                    bottom: 0;
                    margin: auto;
                    height: 0.28rem;
                }

                &.long {
                    width: 3.2rem;
                }
                &.thick {
                    width: 3.8rem;
                    height: 0.8rem;
                }
            }
            .selected {
                border: 0.02rem solid rgb(25, 168, 212);
                background-image: url(./select.png);
                background-size: 0.2rem;
                background-repeat: no-repeat;
                background-position: 99% 46%;
            }
        }
    }
    .explain {
        color: #21288a;
        line-height: 1.2;
        font-size: 0.22rem;
    }
    .confirm {
        position: absolute;
        top: 8.6rem;
        left: 0;
        right: 0;
        margin: auto;
        width: 1.2rem;
    }
}
</style>
